package mapEditor
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.EditAreaView;
	import mapEditor.view.LeftPanelView;
	import mapEditor.view.RightPanelView;
	import mapEditor.view.TopPanelView;
	import mapEditor.view.assetEdit.AssetPanelView;
	import mapEditor.view.mapPreview.MapAreaPreviewView;
	import mapEditor.vo.ModuleType;
	
	import stages.StageReflection;

	public class MapEditorView extends Sprite
	{
		/**
		 *顶部区 
		 */		
		private var _topPanelView:TopPanelView;
		/**
		 *地图预览区 
		 */		
		private var _mapAreaPreviewView:MapAreaPreviewView;
		/**
		 *编辑区域显示遮照 
		 */		
		private var _editAreaMask:Sprite=new Sprite();
		/**
		 *左侧面板 
		 */		
		private var _leftPanelView:LeftPanelView;
		/**
		 *右侧面板 
		 */		
		private var _rightPanelView:RightPanelView;
		/**
		 *中部编辑区 
		 */		
		private var _editAreaView:EditAreaView;
		
		public function MapEditorView()
		{
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.MODULE_TYPE_CHANGE, __onModuleTypeChange);
			StageReflection.stage.addEventListener(Event.RESIZE, __onStateResize);
			
			_topPanelView=new TopPanelView();
			addChild(_topPanelView);
			
			_rightPanelView=new RightPanelView();
			_rightPanelView.x= StageReflection.stageWidth-_rightPanelView.width-2;
			_rightPanelView.y=_topPanelView.height;
			addChild(_rightPanelView);
			
			__onModuleTypeChange(null);
		}
		
		private function __onStateResize(evt:Event):void
		{
			drawEditAreaMask();
			_rightPanelView.x= StageReflection.stageWidth-180-2;
			if((MapEditorManager.instance.mapEditorModel.moduleType==ModuleType.RES_EDIT || MapEditorManager.instance.mapEditorModel.moduleType==ModuleType.SCENE_EDIT) && _editAreaView)
			{
				_editAreaView.areaRec.width=StageReflection.stageWidth-_editAreaView.x-_rightPanelView.width;
				_editAreaView.areaRec.height=StageReflection.stageHeight-_topPanelView.height;
				_editAreaView.resize();
			}
		}
		
		private function __onModuleTypeChange(evt:MapEditorEvent):void
		{
			if(_leftPanelView)_leftPanelView.dispose();
			_leftPanelView=null;			
			if(_mapAreaPreviewView)_mapAreaPreviewView.dispose();
			_mapAreaPreviewView=null;
			if(_editAreaView)_editAreaView.dispose();
			_editAreaView=null;			
			
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					_leftPanelView=new LeftPanelView();
					_leftPanelView.x=2;
					_leftPanelView.y=_topPanelView.height;
					addChild(_leftPanelView);
					
					_editAreaView=new EditAreaView();
					_editAreaView.x=_editAreaMask.x=_leftPanelView.x + _leftPanelView.width+1;
					_editAreaView.y=_editAreaMask.y=_topPanelView.height;
					_editAreaView.areaRec.width=StageReflection.stageWidth-_editAreaView.x-_rightPanelView.width;
					_editAreaView.areaRec.height=StageReflection.stageHeight-_topPanelView.height;
					_editAreaView.show();
					
					drawEditAreaMask();//绘制地图编辑区遮照 
					_editAreaView.mask=_editAreaMask;
					addChild(_editAreaView);
					addChild(_editAreaMask);
					break;
				case ModuleType.SCENE_EDIT:
					_editAreaView=new EditAreaView();
					_editAreaView.x=_editAreaMask.x=1;
					_editAreaView.y=_editAreaMask.y=_topPanelView.height;
					_editAreaView.areaRec.width=StageReflection.stageWidth-_editAreaView.x-_rightPanelView.width;
					_editAreaView.areaRec.height=StageReflection.stageHeight-_topPanelView.height;
					_editAreaView.show();
					
					drawEditAreaMask();//绘制地图编辑区遮照 
					_editAreaView.mask=_editAreaMask;
					addChild(_editAreaView);
					addChild(_editAreaMask);
					break;
				case ModuleType.SCENE_PREVIEW:
					_mapAreaPreviewView=new MapAreaPreviewView();
					_mapAreaPreviewView.x=2;
					_mapAreaPreviewView.y=_topPanelView.height;
					addChild(_mapAreaPreviewView);
					break;
			}
		}
		
		/**
		 *绘制地图编辑区遮照 
		 * 
		 */		
		private function drawEditAreaMask():void
		{
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					_editAreaMask.graphics.clear();
					_editAreaMask.graphics.beginFill(0x000000);
					_editAreaMask.graphics.drawRect(0, 0, StageReflection.stageWidth-_leftPanelView.x-_leftPanelView.width-_rightPanelView.width-2, StageReflection.stageHeight-_topPanelView.height-2);
					_editAreaMask.graphics.endFill();
					break;
				case ModuleType.SCENE_EDIT:
					_editAreaMask.graphics.clear();
					_editAreaMask.graphics.beginFill(0x000000);
					_editAreaMask.graphics.drawRect(0, 0, StageReflection.stageWidth-_rightPanelView.width-2, StageReflection.stageHeight-_topPanelView.height-2);
					_editAreaMask.graphics.endFill();
					break;
			}
		}
		
		public function dispose():void
		{
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStateResize);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.MODULE_TYPE_CHANGE, __onModuleTypeChange);
			
			graphics.clear();
			if(_topPanelView)_topPanelView.dispose();
			_topPanelView=null;
			if(_leftPanelView)_leftPanelView.dispose();
			_leftPanelView=null;
			if(_rightPanelView)_rightPanelView.dispose();
			_rightPanelView=null;
			if(_editAreaView)_editAreaView.dispose();
			_editAreaView=null;			
			if(_mapAreaPreviewView)_mapAreaPreviewView.dispose();
			_mapAreaPreviewView=null;
			if(_editAreaMask)
			{
				_editAreaMask.graphics.clear();
				if(_editAreaMask.parent)_editAreaMask.parent.removeChild(_editAreaMask);
				_editAreaMask=null;
			}
			if(parent)parent.removeChild(this);
		}
	}
}